// heterogeneity in treatment effects //

use "output/data.dta", clear
destring id, replace
xtset id time
foreach depvar in extension sprec rotation manage conserv machine pest l_rev l_exp {
	gen `depvar'_bl = L.`depvar'
}
replace yob = 1970 if yob==.
qui sum exp if time==0, d
gen small = (exp<`r(p50)')
gen CAP1 = CAP*small
gen CAP2 = CAP*(1-small)
gen CAP_ATEG1 = CAP_ATEG*small
gen CAP_ATEG2 = CAP_ATEG*(1-small)

// training //
		
foreach depvar in extension sprec rotation manage conserv machine pest l_exp {

	if `depvar' == l_exp {
		
		reg `depvar' CAP1 CAP2 yob sex res main coop area L.`depvar' small if time==1 & CAP_ATEG==0, robust
	
	}

	else {	
		
		reg `depvar' CAP1 CAP2 yob sex res main coop area L.`depvar' small if time==1 & CAP_ATEG==0, robust
		
	}
		
	foreach model in 1 2 {	
		
		scalar b`model'`depvar' = _b[CAP`model']
		loc b`model'`depvar': display %4.3f b`model'`depvar'
		scalar se`model'`depvar' = _se[CAP`model']
		loc se`model'`depvar': display %4.3f se`model'`depvar'
		loc t = _b[CAP`model']/_se[CAP`model']
		loc p = 2*ttail(e(df_r),abs(`t'))
		scalar n`model'`depvar' = e(N)
		loc obs`model'`depvar': display %3.0f n`model'`depvar' 
		loc star`model'`depvar' = cond(`p'<0.01,"***",cond(`p'<0.05,"**",cond(`p'<0.10,"*","")))
				
	}
	
	qui test CAP1 = CAP2
	
	scalar test`depvar' = r(p) 
	loc test`depvar': display %4.3f test`depvar'

}		
		
		
file open Table using "tables/tableS8.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{Effects of Training by Initial Expenditures}" _n
file write Table "\label{tab:het1}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table " & $<$ Median & $>$ Median & Equality Test & N \\" _n		
file write Table " & & & (p-value) &  \\" _n		
file write Table "\midrule" _n
file write Table "Technical Assistance (0/1) & `b1extension'`star1extension' & `b2extension'`star2extension' & `testextension' & `obs1extension' \\" _n
file write Table " & (`se1extension') & (`se2extension') & & \\" _n	
file write Table "Pasture Restoration (\%) & `b1sprec'`star2sprec' & `b2sprec'`star2sprec' & `testsprec' & `obs1sprec' \\" _n
file write Table " & (`se1sprec') & (`se2sprec') & & \\" _n
file write Table "Rotational Grazing (0/1) & `b1rotation'`star1rotation' & `b2rotation'`star2rotation' & `testrotation' & `obs1rotation' \\" _n
file write Table " & (`se1rotation') & (`se2rotation')  & & \\" _n
file write Table "Management Practices & `b1manage'`star1manage' & `b2manage'`star2manage' & `testmanage' & `obs1manage' \\" _n
file write Table " & (`se1manage') & (`se2manage') & & \\" _n	
file write Table "Conservation Practices & `b1conserv'`star1conserv' & `b2conserv'`star2conserv' & `testconserv' & `obs1conserv' \\" _n
file write Table " & (`se1conserv') & (`se2conserv') & & \\" _n		
file write Table "Machine Use (0/1) & `b1machine'`star1machine' & `b2machine'`star2machine' & `testmachine' & `obs1machine' \\" _n
file write Table " & (`se1machine') & (`se2machine') & & \\" _n	
file write Table "Pesticide Use (0/1) & `b1pest'`star1pest' & `b2pest'`star2pest' & `testpest' & `obs1pest' \\" _n
file write Table " & (`se1pest') & (`se2pest') & & \\" _n					
file write Table "log(Expenditures) & `b1l_exp'`star1l_exp' & `b2l_exp'`star2l_exp' & `testl_exp' & `obs1l_exp' \\" _n
file write Table " & (`se1l_exp') & (`se2l_exp') & & \\" _n  
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Each row reports the results for a different outcome. Column 1 reports the effects of training for producers w/ initial expenditures below the median. Column 2 reports the effects of training for producers w/ initial expenditures above the median. Column 3 tests whether the effects are statistically different. Robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table

// technical assistance //
		
foreach depvar in extension sprec rotation manage conserv machine pest l_exp {

	if `depvar' == l_exp {
		
		reg `depvar' CAP_ATEG1 CAP_ATEG2 yob sex res main coop area L.`depvar' small if time==1 & CTRL==0, robust
	
	}

	else {	
		
		reg `depvar' CAP_ATEG1 CAP_ATEG2 yob sex res main coop area L.`depvar' small if time==1 & CTRL==0, robust
		
	}
		
	foreach model in 1 2 {	
		
		scalar b`model'`depvar' = _b[CAP_ATEG`model']
		loc b`model'`depvar': display %4.3f b`model'`depvar'
		scalar se`model'`depvar' = _se[CAP_ATEG`model']
		loc se`model'`depvar': display %4.3f se`model'`depvar'
		loc t = _b[CAP_ATEG`model']/_se[CAP_ATEG`model']
		loc p = 2*ttail(e(df_r),abs(`t'))
		scalar n`model'`depvar' = e(N)
		loc obs`model'`depvar': display %3.0f n`model'`depvar' 
		loc star`model'`depvar' = cond(`p'<0.01,"***",cond(`p'<0.05,"**",cond(`p'<0.10,"*","")))
				
	}
	
	qui test CAP_ATEG1 = CAP_ATEG2
	
	scalar test`depvar' = r(p) 
	loc test`depvar': display %4.3f test`depvar'

}		
		
		
file open Table using "tables/tableS9.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{Effects of Technical Assistance by Initial Expenditures}" _n
file write Table "\label{tab:het2}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table " & $<$ Median & $>$ Median & Equality Test & N \\" _n	
file write Table " & & & (p-value) &  \\" _n	
file write Table "\midrule" _n
file write Table "Technical Assistance (0/1) & `b1extension'`star1extension' & `b2extension'`star2extension' & `testextension' & `obs1extension' \\" _n
file write Table " & (`se1extension') & (`se2extension') & & \\" _n	
file write Table "Pasture Restoration (\%) & `b1sprec'`star2sprec' & `b2sprec'`star2sprec' & `testsprec' & `obs1sprec' \\" _n
file write Table " & (`se1sprec') & (`se2sprec') & & \\" _n
file write Table "Rotational Grazing (0/1) & `b1rotation'`star1rotation' & `b2rotation'`star2rotation' & `testrotation' & `obs1rotation' \\" _n
file write Table " & (`se1rotation') & (`se2rotation')  & & \\" _n
file write Table "Management Practices & `b1manage'`star1manage' & `b2manage'`star2manage' & `testmanage' & `obs1manage' \\" _n
file write Table " & (`se1manage') & (`se2manage') & & \\" _n	
file write Table "Conservation Practices & `b1conserv'`star1conserv' & `b2conserv'`star2conserv' & `testconserv' & `obs1conserv' \\" _n
file write Table " & (`se1conserv') & (`se2conserv') & & \\" _n		
file write Table "Machine Use (0/1) & `b1machine'`star1machine' & `b2machine'`star2machine' & `testmachine' & `obs1machine' \\" _n
file write Table " & (`se1machine') & (`se2machine') & & \\" _n	
file write Table "Pesticide Use (0/1) & `b1pest'`star1pest' & `b2pest'`star2pest' & `testpest' & `obs1pest' \\" _n
file write Table " & (`se1pest') & (`se2pest') & & \\" _n					
file write Table "log(Expenditures) & `b1l_exp'`star1l_exp' & `b2l_exp'`star2l_exp' & `testl_exp' & `obs1l_exp' \\" _n
file write Table " & (`se1l_exp') & (`se2l_exp') & & \\" _n  
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Each row reports the results for a different outcome. Column 1 reports the effects of training for producers w/ initial expenditures below the median. Column 2 reports the effects of training for producers w/ initial expenditures above the median. Column 3 tests whether the effects are statistically different. Robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table
